package com.dongge.action;

import com.dongge.annotation.HasRole;
import com.dongge.entity.User;
import com.dongge.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * Created by Administrator on 2016/7/9.
 */
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport {

    private long id;
    private String username;
    private String password;
    private String returnUrl;

    @Autowired
    private UserService userService;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getReturnUrl() {
        return returnUrl;
    }

    public void setReturnUrl(String returnUrl) {
        this.returnUrl = returnUrl;
    }

    /**
     * 注册成功就转到登录页面
     * 注册失败显示错误信息
     * @return
     */
    public String register(){

        if(userService.register(username,password)){
            return "login";
        }
        return "failure";
    }

    /**
     * 登录成功就转向到returnUrl，如果returnUrl==null 就转到main主页
     * 登录失败就不转向
     * @return
     */
    public String login(){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try{
            subject.login(token);
        }catch (AuthenticationException e){
            ActionContext.getContext().put("error","用户名或密码错误");
            return "failure";
        }
        return returnUrl!=null?"returnUrl":"main";
    }

    /**
     * 退出就要退出到首页
     * 没登录的话就待在当前页面
     * @return
     */
    public String loginout(){
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            subject.logout();
            return "main";
        }
        return returnUrl!=null?"returnUrl":"main";
    }

    /**
     * 显示用户列表
     * @return
     */
    @HasRole("admin")
    public String list(){
        List<User> users= userService.getUsers();
        ActionContext.getContext().put("users",users);
        return "list";
    }

    @HasRole("admin")
    public String delete(){
        userService.delete(id);
        return "list";
    }

    @HasRole("admin")
    public String add(){
        return "add";
    }

    /**
     *
     * 管理员创建用户不需要提供密码,使用的都是初始密码
     */
    @HasRole("admin")
    public String create(){
        User user = new User();
        user.setUsername(username);
        user.setPassword("123456");
        userService.save(user);
        return "list";
    }

    public String edit(){
        User user = userService.getUserById(id);
        ActionContext.getContext().getValueStack().push(user);
        return "edit";
    }

    public String update(){
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        userService.update(user);
        return "list";
    }

    public String reset(){
        userService.reset(id);
        return "list";
    }

    @Override
    public String execute() throws Exception {
        return super.execute();
    }
}
